<template>
  <div class="article-container">
    <van-button
      :icon="attitude === 1 ? 'good-job' : 'good-job-o'"
      :class="{
        liked: attitude === 1,
      }"
      @click="onCollect"
      :loading="loading"
      class="van-icon"
    >
    </van-button>
  </div>
</template>

<script>
import { deleteLike, addLike } from "@/api/article";
export default {
  name: "LikeArticle",
  data() {
    return {
      loading: false,
    };
  },
  props: {
    attitude: {
      type: Number,
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  methods: {
    async onCollect() {
      this.loading = true;
      try {
        //代表无状态,没有点赞
        let status = -1;
        if (this.attitude === 1) {
          await deleteLike(this.articleId);
        } else {
          await addLike(this.articleId);
          //代表点赞状态
          status = 1;
        }
        this.$emit("input", status);
        this.$toast.success(status === 1 ? "点赞成功" : "取消点赞");
      } catch (error) {
        this.$toast.fail("操作失败");
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.article-container {
  .van-icon {
    color: #e5645f !important;
  }
}
</style>
